package operate;
import book.Book;
import book.BookList;
import user.User;

import java.util.Scanner;
public class Love implements IOperate{
    /**
     * 功能：点赞书籍
     * 让用户再次确认是否要收藏
     * 点赞数++
     */
    @Override
    public void operate(User user, BookList bookList) {
        //点赞操作
        if( loveOperate(bookList)) {
            int p = user.getPoint();//点赞成功积分+1
            user.setPoint(++p);
        }
    }
    //点赞操作
    private Boolean loveOperate(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        String bookName;
        Book book;
        while (true) {
            System.out.println("请输入你要点赞的书籍:");
            //输入要点赞的书籍名称
            bookName = sc.nextLine();
            //判断是否有这本书
            book = findbook(bookName, bookList);
            if (book == null) {
                System.out.println("没有这本书");
                return false;
            }
            int love = book.getLove();//点赞成功积分+1
            love++;
            book.setLove(love);
            //bookList 中存储的是可变对象，通过获取对象引用并修改其状态，会直接影响 ArrayList 中的元素。
            //这就是为什么只执行book.setLove(love)，而不把book重新写进 bookList也会使bookList中的元素发生变化
            System.out.println("点赞成功");
            return true;
        }
    }
    //查找书籍
    private Book findbook(String bookName, BookList bookList) {
        Book book;
        //循环遍历书籍
        for (int i = 0; i < bookList.bookLists.size(); i++) {
            if (bookList.bookLists.get(i).getName().equals(bookName)) {
                book = bookList.bookLists.get(i);
                return book;
            }
        }
        return null;
    }
}
